package com.bucg.cjcloud.gb28181.sip.conf;

import com.bucg.cjcloud.gb28181.sip.SipServerInfo;
import com.bucg.cjcloud.gb28181.sip.process.DelegatingRequestProcess;
import com.bucg.cjcloud.gb28181.sip.process.RequestProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sip.InvalidArgumentException;
import javax.sip.ListeningPoint;
import javax.sip.ObjectInUseException;
import javax.sip.PeerUnavailableException;
import javax.sip.SipFactory;
import javax.sip.SipProvider;
import javax.sip.SipStack;
import javax.sip.TransportAlreadySupportedException;
import javax.sip.TransportNotSupportedException;
import javax.sip.address.Address;
import javax.sip.address.AddressFactory;
import javax.sip.address.SipURI;
import javax.sip.header.ContactHeader;
import javax.sip.header.HeaderFactory;
import javax.sip.message.MessageFactory;
import java.text.ParseException;
import java.util.List;
import java.util.Properties;
import java.util.TooManyListenersException;

@Configuration
public class SipLayerConfiguation {

    @Bean
    public HeaderFactory createHeaderFactory() throws PeerUnavailableException {

        return SipFactory.getInstance().createHeaderFactory();

    }

    @Bean
    public AddressFactory createAddressFactory() throws PeerUnavailableException {

        return SipFactory.getInstance().createAddressFactory();
    }

    @Bean
    public MessageFactory createMessageFactory() throws PeerUnavailableException {

        return SipFactory.getInstance().createMessageFactory();
    }

    @Bean(name = "fromAddress")
    public Address createAddress(AddressFactory addressFactory, SipServerInfo sipinfo) throws ParseException {
        SipURI from = addressFactory.createSipURI(sipinfo.getUsername(), sipinfo.getIp() + ":" + sipinfo.getPort());
        Address fromNameAddress = addressFactory.createAddress(from);
        fromNameAddress.setDisplayName(sipinfo.getUsername());
        System.out.println("aa");
        return fromNameAddress;
    }

    @Bean(name = "contactHeader")
    public ContactHeader getContactHeader(AddressFactory addressFactory, SipServerInfo sipinfo, HeaderFactory headerFactory) throws ParseException {
        SipURI contactURI = addressFactory.createSipURI(sipinfo.getUsername(), sipinfo.getPort() + "");
        Address contactAddress = addressFactory.createAddress(contactURI);
        contactAddress.setDisplayName(sipinfo.getUsername());
        ContactHeader contactHeader = headerFactory
                .createContactHeader(contactAddress);
        System.out.println("bb");
        return contactHeader;
    }

    /**
     * 未设置addlistener
     * */
    @Bean
    public SipProvider createSipProvider(SipServerInfo info) throws TooManyListenersException, TransportNotSupportedException, InvalidArgumentException, PeerUnavailableException, ObjectInUseException {
        String ip = info.getIp();
        int port = info.getPort();
        SipFactory sipFactory = SipFactory.getInstance();
        Properties properties = new Properties();
        properties.setProperty("javax.sip.STACK_NAME", "GB28181-2016");
        properties.setProperty("javax.sip.IP_ADDRESS", ip);
        SipStack sipStack = sipFactory.createSipStack(properties);
        ListeningPoint tcp = sipStack.createListeningPoint(ip, port, "tcp");
        ListeningPoint udp = sipStack.createListeningPoint(ip, port, "udp");

        SipProvider sipProvider = sipStack.createSipProvider(tcp);
        try {
            sipProvider.addListeningPoint(udp);
        } catch (TransportAlreadySupportedException e) {
            e.printStackTrace();
        }
        // sipProvider = sipStack.createSipProvider(udp);

        return sipProvider;
    }

    @Autowired
    List<RequestProcess> requestProcesses;
    @Bean
    @Primary
    public RequestProcess getDelegatingRequestProcess() {
        return new DelegatingRequestProcess(requestProcesses);

    }
}
